<template>
  <a-drawer
    title="新增考试科目信息"
    :width="windowWidth"
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="testManageAddVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}">
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.sszym" @change="zyChange">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="研究方向">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.yjfxm" @change="fxChange">
                <a-select-option v-for="i in selectMenu.fx" :key="i.id" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="导师">
              <a-select  mode="multiple" :allowClear="true" showSearch optionFilterProp="children" v-decorator="['dsxxmIds',{initValue:''}]" >
                <a-select-option v-for="i in selectMenu.ds" :key="i.id" :value="i.id">{{ i.gh+'-'+i.xm }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目类型">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.kmlxm" @change="kmChange">
                <a-select-option v-for="i in TYPE0392" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目编码">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.bsckkmId" >
                <a-select-option v-for="i in selectMenu.bm" :key="i.id" :value="i.id">{{ i.kmbm+'-'+i.kmmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
  // import moment from 'moment'
  import {STable} from '~'
  import {mapState} from 'vuex'

  const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0392']
export default {
  name: 'TestManageAdd',
  props: {
    testManageAddVisiable: {
      default: false
    }
  },
  components: {
    STable
  },
  data () {
    return {
      formItemLayout,
      windowWidth:"450",
      loading: false,
      postParams: {
       sszym:'',
       ssyxm:'',
       yjfxm:'',
       dsxxmIds:'',
       kmlxm:'',
       bsckkmId:''
      },
      selectMenu: {
        yx: [], // 院系
        zy: [], //专业
        fx: [], //方向
        ds: [], //导师
        bm: []
      },
      doctoralAdmissionYear:'',
      form: this.$form.createForm(this)
    }
  },

  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0392: state => state.app['TYPE0392'] // 年级
    })
  },
  mounted(){
    this.windowWidth = document.body.clientWidth>450?450:document.body.clientWidth
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
      this.$api.base.getSchoolYear().then(res=>{
        this.doctoralAdmissionYear=res.data.doctoralAdmissionYear;
      })
    },
    reset () {
      this.data = []
    },
    // 选择院系
    yxChange (code) {
      this.$set(this.postParams,'sszym','')
      this.$set(this.postParams,'yjfxm','')
      this.form.setFieldsValue({'dsxxmIds':[]})
      this.$set(this.selectMenu,'zy','')
      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      if(code){
        console.log(111)
       this.$api.base.majorAll({lsdwh: code,zycc:'3'}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
      }
    },
    //选择专业
    zyChange(code){
      this.$set(this.postParams,'yjfxm','')
       this.form.setFieldsValue({'dsxxmIds':[]})

      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      if(code){
      let params={
        ssyxm:this.postParams.ssyxm,
        sszym:this.postParams.sszym,
        xwccm:3,
        zsxn:this.doctoralAdmissionYear
      }
      this.$api.base.researchAll(params).then(res=>{
        this.$set(this.selectMenu,'fx',res.data)
      })
      }
    },
    //选择方向
    fxChange(code){
       this.form.setFieldsValue({'dsxxmIds':[]})
      this.$set(this.selectMenu,'ds','')
      if(code){
      let params={
        yjfxm:this.postParams.yjfxm,
        zym:this.postParams.sszym,
        zsxn:this.doctoralAdmissionYear,
        ssyxm:this.postParams.ssyxm
      }
      this.$api.base.getGuide(params).then(res=>{
        this.$set(this.selectMenu,'ds',res.data)
      })
      }
    },
    //选择科目
    kmChange(code){
      this.$set(this.postParams,'bsckkmId','')
      this.$set(this.selectMenu,'bm')
      if(code){
      let params={
        ssyxm:this.postParams.ssyxm,
        kmlxm:this.postParams.kmlxm
      }
      this.$api.base.getCode(params).then(res=>{
        this.$set(this.selectMenu,'bm',res.data)
      })
      }
    },
    // 选择招生年度
    zsndChange () {

    },
    // 关闭
    onClose () {
      this.reset()
      this.$set(this.postParams,'ssyxm','')
      this.$set(this.postParams,'sszym','')
      this.$set(this.postParams,'yjfxm','')
       this.$set(this.selectMenu,'zy','')
      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      this.form.setFieldsValue({'dsxxmIds':[]})
      this.$emit('close')
    },
    // 保存
    handleSubmit () {
      let params = Object.assign(this.postParams,this.form.getFieldsValue())
       this.$api.doctorRecruit
        .examinationAdd(this.postParams)
        .then(res => {
          // this.$message.success(res.msg)
          this.$emit("success");
          this.postParams.ssyxm=''
          this.postParams.sszym=''
           this.form.setFieldsValue({'dsxxmIds':[]})
          this.postParams.yjfxm=''
          this.postParams.kmlxm=''
          this.postParams.bsckkmId=''
        })
        .finally(() => {
          this.loadingBtn = false;

        });
    },
    handleDelete (key) {
      const dataSource = [...this.data]
      this.data = dataSource.filter(item => item.key !== key)
    }
  }
}
</script>
